﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace 外链整改
{


    public partial class Form1 : Form
    {
        public List<LinkObj> list = new List<LinkObj>();

        public Form1()
        {
            InitializeComponent();
        }

        string path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\";

        string selectProblem = string.Empty;
        private DataSet GetSMSList(string file)
        {
            string strCon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + file + ";Extended Properties=Excel 8.0";
            DataSet ds;
            OleDbConnection conn = new OleDbConnection(strCon);
            string sql = "select * from [Sheet1$]";
            conn.Open();
            OleDbDataAdapter myCommand = new OleDbDataAdapter(sql, strCon);
            ds = new DataSet();
            myCommand.Fill(ds, "[Sheet1$]");
            conn.Close();
            return ds;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                DataSet ds = GetSMSList(openFileDialog1.FileName);
                list.Clear();
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(new LinkObj() { Url = row[1].ToString(), Problem = row[2].ToString() });
                }
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            WebClient web = new WebClient();

            progressBar1.Maximum = list.Count;
            progressBar1.Value = 0;
            int Index = 0;

            foreach (LinkObj obj in list)
            {
                string urlPath = obj.Url.Substring(0, obj.Url.LastIndexOf(@"/"));
                urlPath = urlPath.Substring(84).Replace(@"/", @"\");
                string root = path + urlPath + @"\";
                string bkroot = path + urlPath.Substring(0, urlPath.IndexOf(@"\")) + @"备份" + urlPath.Substring(urlPath.IndexOf(@"\")) + @"\";

                if (!Directory.Exists(root))
                    Directory.CreateDirectory(root);

                if (!Directory.Exists(bkroot))
                    Directory.CreateDirectory(bkroot);


                string filename = obj.Url.Substring(obj.Url.LastIndexOf(@"/") + 1);

                if (!File.Exists(root + filename))
                    web.DownloadFile("https://" + obj.Url, root + filename);

                list[Index].LocalFile = root + filename;

                if (!File.Exists(bkroot + filename))
                    web.DownloadFile("https://" + obj.Url, bkroot + filename);

                progressBar1.Value = progressBar1.Value + 1;
                Index++;
            }


            listBox1.DataSource = null;
            listBox1.DataSource = list;
            listBox1.ValueMember = "Problem";
            listBox1.DisplayMember = "Url";

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string file = list[listBox1.SelectedIndex].LocalFile;
            selectProblem = listBox1.SelectedValue.ToString();


            txtContent.LoadFile(file, RichTextBoxStreamType.PlainText);

            SearchText(txtContent, selectProblem);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            txtContent.Find(selectProblem, RichTextBoxFinds.WholeWord);
        }


        public void SearchText(RichTextBox rich, string strKey)
        {
            int start = 0;
            int end = rich.Text.Length;

            int index = rich.Find(strKey, start, end, RichTextBoxFinds.None);

            int searchCount = 0;

            while (index != -1)
            {
                rich.SelectionFont = new Font(rich.SelectionFont, FontStyle.Underline | FontStyle.Bold);
                rich.SelectionColor = Color.Red;
                start = index + strKey.Length;
                searchCount++;
                index = rich.Find(strKey, start, end, RichTextBoxFinds.None);
            }

            lblSearch.Text = "外链地址： " + strKey +  "  搜索到 " + searchCount.ToString() + " 个";

        }

        private void txtContent_TextChanged(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int Index = listBox1.SelectedIndex;
            string file = list[Index].LocalFile;

            txtContent.SaveFile(file, RichTextBoxStreamType.PlainText);
            btnSave.Enabled = false;
        }
    }
    public class LinkObj
    {
        public string Url { get; set; }
        public string Problem { get; set; }

        public string LocalFile { get; set; }
        public override string ToString()
        {
            return Url.Substring(84);
        }
    }

}
